# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
import execjs
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
丁香园-论坛
https://www.dxy.cn/bbs/newweb/pc/board/188?order=2&tpg=1
http://www.dxy.cn/bbs/board/188 旧版
http://i.dxy.cn/profile/fijiji 旧版
账号:18241255868
密码:gkx55868

"""


class LunTan_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15'
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Cookie': 'route_bbs=4b7df70fe96acfe89c32b68456fd2ed3; JUTE_SESSION_ID=fa40c897-8efe-48d2-afbb-474f6acda7ca; dxy_da_cookie-id=b631f019-d91e-4aff-97a4-c7b2f30e604b; JUTE_TOKEN=0af97373-a1cd-427e-bda1-7ddf467b6f91; route=fe9340375d898dc58dc46edbbdbb48f8; cms_token=045a26a0-4852-4882-beee-289a1562a24d; DXY_USER_GROUP=72; __auc=63a0f4ab170f8039c0d41ec9875; __asc=63a0f4ab170f8039c0d41ec9875; _ga=GA1.2.1635620379.1584708951; _gid=GA1.2.1778138882.1584708967; CMSSESSIONID=2A92C4CE960D6FEC7A2F28905B02B42A-n1; Hm_lvt_8a6dad3652ee53a288a11ca184581908=1584708947,1584709209,1584709233; JUTE_BBS_DATA=3b70324384c914d3065d388c3af29eba0f94776bff07e56446becaa41e8af3bca986948c8d2c050ec8f31af9a82ac5fdb32898816bf548e5f98b932000475cb4c9e5081db97f9444d041e8d083c55c78; CLASS_CASTGC=8b664bba8b2d41e9afa91e0e78f20c498a68f970c8a369b9e4c17e7c42aa8781611269467f28d73603fbc5b97699e612b8d95140ba8bc7007d5a90ea1d155af90888742bb323fa6e42b214f80a15a28e91f2bb3a09112939620c039521641fb726cdc172c23d7b20b6a5ed266b983c5e4a9f6f3115b0d40c5c1a7a1ed76289dcb523b6ecf7e3a2b9a63a6fb05db24bd6bfb11f4557e1e9d0df9be865ac7795fee09b6a80354f92cc946e926539019dd10c10c40942f845cd7e29a59c7e043f1ad7e091f0a08bf6de5d7e061d4db4605de122346e5c5429e777eb69ab01cc8684deca6d748a76768ca956a06a5e4772ab3d6e299956fa3d9d536f725d3531de09; __utma=1.1635620379.1584708951.1584709402.1584709402.1; __utmc=1; __utmz=1.1584709402.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; ifVisitOldVerBBS=true; JUTE_SESSION=8d4d623f31d425d94416dd42a6ebb0833ff5c466d1ad1b85e30586fb3f139c73d5f3e3a3a0fac35076a0fe7bdd806dab8c9cdf2f31cd37b271a125386d1c8d9bcf048092a56408aa; __utmb=1.17.9.1584709703550; Hm_lpvt_8a6dad3652ee53a288a11ca184581908=1584710524',
            'DNT': '1',
            'Host': 'www.dxy.cn',
            'Referer': 'http://www.dxy.cn/bbs/board/188',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
        }
        self.address = self.get_address_json()


    def get_contents(self, boardId, cursor):
        """
        获取帖子列表
        :param boardId: 版块号
        :param cursor: 页号
        :return:
        """
        # 构造加密参数 noncestr,sign
        appSignKey = "appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3"
        noncestr = self.get_noncestr()
        postData = f"boardId={boardId}" + f"&noncestr={noncestr}" + f"&orderType=2&pageNum={cursor}&pageSize=30&postType=0&subBoardId=0&timestamp={int(time.time() * 1000)}"
        postData1 = appSignKey + "&" + postData
        sign = self.get_sing(str(postData1))
        postData = postData + f"&sign={sign}" + f"&noncestr={noncestr}"
        url = "https://www.dxy.cn/bbs/newweb/board/post/list"
        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)
        for d in data_json['data']['result']:
            dict_data = dict()
            dict_data['id'] = d['id']  # 帖子id
            dict_data['subject'] = d['subject']  # 主题
            dict_data['simpleBody'] = d['simpleBody']  # 内容
            dict_data['userId'] = d['userId']  # 楼主id
            dict_data['username'] = d['username']  # 楼主
            dict_data['replies'] = d['replies']  # 回复数量
            dict_data['votes'] = d['votes']  # 点赞数量
            dict_data['reads'] = d['reads']  # 浏览数量
            dict_data['createTime'] = datetime.fromtimestamp(int(d['createTime']) / 1000) \
                .strftime("%Y-%m-%d %H:%M:%S")  # 发帖时间
            dict_data['lastPostTime'] = datetime.fromtimestamp(int(d['lastPostTime']) / 1000)\
                .strftime("%Y-%m-%d %H:%M:%S")  # 最后回复时间
            dict_data['lastPostName'] = d['lastPostName']  # 最后回复用户

            print(str(dict_data))
            # 回复内容
            self.get_comments(d['id'], 1)

    def get_comments(self, postId, cursor):
        """
        获取回复内容
        :param postId: 帖子id
        :param cursor: 页号
        :return:
        """
        # 构造加密参数 noncestr,sign
        appSignKey = "appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3"
        noncestr = self.get_noncestr()
        postData = f"onlyQuality=false" + f"&pageNum={cursor}&pageSize=20&postId={postId}&timestamp={int(time.time() * 1000)}"
        postData1 = appSignKey + f"&noncestr={noncestr}&" + postData
        sign = self.get_sing(str(postData1))
        postData = postData + f"&sign={sign}" + f"&noncestr={noncestr}"
        url = "https://www.dxy.cn/bbs/newweb/post/reply/list"
        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)
        total = data_json['data']['total']  # 总数量
        for d in data_json['data']['result']:
            dict_data = dict()
            dict_data['id'] = d['id']  # 帖子id
            dict_data['body'] = d['body']  # 内容
            dict_data['createTime'] = datetime.fromtimestamp(int(d['createTime']) / 1000) \
                .strftime("%Y-%m-%d %H:%M:%S")  # 回复时间
            dict_data['userId'] = d['userId']  # 用户id
            dict_data['body'] = d['postUser']['nickname']  # 用户名
            dict_data['bbsStatusTitle'] = d['postUser']['bbsStatusTitle']  # 级别
            dict_data['replies'] = d['replies']  # 回复数量
            dict_data['votes'] = d['votes']  # 点赞数量
            dict_data['favorites'] = d['favorites']  # 收藏数量
            print(str(dict_data))
        pageNo = 1
        if total > 20:
            if total % 20 != 0:
                pageNo = total // 20 + 1
            else:
                pageNo = total // 20
        if cursor < pageNo:
            self.get_comments(postId, cursor + 1)

    def get_user(self, userid):
        """
        获取用户信息
        :param userid:用户id
        :return:
        """
        # 构造加密参数 noncestr,sign
        appSignKey = "appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3"
        noncestr = self.get_noncestr()
        postData = f"timestamp={int(time.time() * 1000)}&userId={userid}"
        postData1 = appSignKey + f"&noncestr={noncestr}&" + postData
        sign = self.get_sing(str(postData1))
        postData = postData + f"&sign={sign}" + f"&noncestr={noncestr}&userId={userid}"
        url = "https://www.dxy.cn/bbs/newweb/personal-page/user-info"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)

        dict_data = dict()
        dict_data['username'] = data_json['data']['username']  # 用户名
        dict_data['userLevel'] = data_json['data']['userLevel']  # 用户等级
        dict_data['bbsStatusTitle'] = data_json['data']['bbsStatusTitle']  # 论坛等级
        dict_data['signature'] = data_json['data']['signature']  # 简介
        dict_data['titles'] = "|".join(data_json['data']['titles']['doctorIdentify'])  # 论坛信息
        dict_data['locatedCity'] = self.get_address(str(data_json['data']['locatedCity']))  # 城市


        # 构造加密参数 noncestr,sign
        appSignKey = "appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3"
        noncestr = self.get_noncestr()
        postData = f"timestamp={int(time.time() * 1000)}&userId={userid}"
        postData1 = appSignKey + f"&noncestr={noncestr}&" + postData
        sign = self.get_sing(str(postData1))
        postData = postData + f"&sign={sign}" + f"&noncestr={noncestr}&userId={userid}"
        url = "https://www.dxy.cn/bbs/newweb/user/data"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        dict_data['followings'] = data_json['data']['followings']  # 关注
        dict_data['followers'] = data_json['data']['followers']  # 粉丝
        dict_data['bbsScore'] = data_json['data']['bbsScore']  # 积分
        dict_data['postClicked'] = data_json['data']['postClicked']  # 帖子被浏览
        dict_data['bbsVotes'] = data_json['data']['bbsVotes']  # 获赞
        dict_data['postPicked'] = data_json['data']['postPicked']  # 被收藏
        print(str(dict_data))

    # 解密noncestr
    def get_noncestr(self):
        with open("decrypt.js", 'r', encoding="utf-8") as f:
            js_content = f.read()
            ctx = execjs.compile(js_content)
            noncestr = ctx.call("u1", 8, "number")
            # noncestr = ctx.call("u1", "boardId=188&subBoardId=0&postType=0&orderType=2&pageNum=6&pageSize=30}", "4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3")
        return noncestr

    # 解密sign
    def get_sing(self, params):
        with open("decrypt.js", 'r', encoding="utf-8") as f:
            js_content = f.read()
            ctx = execjs.compile(js_content)
            # sign = ctx.call("u","appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3&boardId=188&noncestr=16918870&orderType=2&pageNum=10&pageSize=30&postType=0&subBoardId=0&timestamp=1584761344473", "")
            sign = ctx.call("u", params, "")
        return sign

    # 获取地址列表
    def get_address_json(self):
        html = HttpUtils.do_request("GET", "https://assets.dxycdn.com/core/widgets/cascading-list-v2/data/location.js?t=146738", "", "")
        str_json = re.findall(r"var dataLocation = (.*?);", html.text, re.S)[0]
        data_json = json.loads(str_json)
        return data_json

    # 解析用户地址
    def get_address(self, citycode):
        tmd_p = citycode[:3]
        for p_1 in self.address:
            if str(p_1['key']) == tmd_p + "000":
                p1 = p_1['label']
                for p_2 in p_1['children']:
                    if str(p_2['key']) == citycode[:4] + "00":
                        p2 = p_2['label']
                        for p_3 in p_2['children']:
                            if str(p_3['key']) == citycode:
                                p3 = p_3['label']
                                return p1 + "|" + p2 + "|" + p3


if __name__ == '__main__':
    luntan = LunTan_Get()
    # 获取用户信息
    luntan.get_user("157178")
    for i in range(1, 100):
        print(f"============{i}=============")
        # 获取帖子列表
        luntan.get_contents(188, i)